package com.hrp.service;

import com.hrp.domain.Jxkhzq;
import com.hrp.domain.ResponseData;
import com.hrp.domain.TransportRecord;
import com.hrp.domain.UimmsPrice;
import com.hrp.hrpdao.PriceSettingDao;
import com.hrp.hrpdao.TransportRecordDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019-04-27.
 */
@Service
public class TransportRecordService {

    private static final Logger logger = LoggerFactory.getLogger(TransportRecordService.class);

    @Autowired
    private TransportRecordDao transportRecordDao;

    @Autowired
    private PriceSettingDao priceSettingDao;

    @Value("${hrp.showflag}")
    public  String showflag;


    public Map<String, Object> getTransportRecordByPage(Integer page, Integer rows,String username) {




        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        try{
            //设置数据查看权限
            List<Integer> showList = new ArrayList<>();
            showList.add(0);
            Integer show  = showflag.indexOf(username);
            if(show>-1){
                showList.add(1);
            }


            Integer total = transportRecordDao.getTotalCount(showList);
            Integer startIndex = (page-1)*rows;
            Integer endIndex = page*rows;
            List<Integer> listStr = new ArrayList<>();
            for(Integer i=(startIndex+1);i<=endIndex;i++){
                listStr.add(i);
            }
            List<TransportRecord> list = transportRecordDao.getTransportRecordByPage(listStr,showList);
            jsonMap.put("total", total);//total键 存放总记录数，必须的
            jsonMap.put("rows", list);//rows键 存放每页记录 list
            return jsonMap;
        }catch (Exception e) {
            e.printStackTrace();
            logger.info("获取分页账期数据失败");
            return jsonMap;
        }
    }


    public ResponseData saveTransportRecordData(TransportRecord transportRecord) {
        StringBuilder sbMsg = new StringBuilder();
        try{
            List<UimmsPrice> priceList = priceSettingDao.getPriceSettingByInfo(transportRecord.getVarietiesId(),transportRecord.getSupplierId().toString(),transportRecord.getReceiverId().toString(),transportRecord.getSendTime());

            if(priceList.size()==0){
                sbMsg.append("没有匹配的单价规则");
            }else if(priceList.size()==1){
                sbMsg.append("单价规则更新单价成功");
                transportRecord.setPrice(priceList.get(0).getPrice());
            }else{
                sbMsg.append("匹配的单价规则过多大于一条");
            }

            TransportRecord transportRecordOld = transportRecordDao.getTransportRecordData(transportRecord.getId());
            if(transportRecordOld==null){
                transportRecordDao.saveTransportRecordData(transportRecord);
            }else{
                transportRecordDao.updateTransportRecordData(transportRecord);
            }
            return ResponseData.build(200,"保存成功("+sbMsg.toString()+")");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.build(500,"保存失败("+sbMsg.toString()+")");
        }
    }

    public ResponseData updateTransportRecordDataPrice(Integer price, String sendTimeKsrq, String sendTimeJsrq, String varietiesIdStr, String supplierIdStr, String receiverIdStr) {
        try{

            List<Integer> varietiesIdList = new ArrayList<>();
            String[] varietiesIdArr= varietiesIdStr.split(",");
            for(int i=0 ;i<varietiesIdArr.length;i++){
                varietiesIdList.add(Integer.parseInt(varietiesIdArr[i]));
            }

            List<Integer> supplierIdList = new ArrayList<>();
            String[] supplierIdArr= supplierIdStr.split(",");
            for(int i=0 ;i<supplierIdArr.length;i++){
                supplierIdList.add(Integer.parseInt(supplierIdArr[i]));
            }

            List<Integer> receiverIdList = new ArrayList<>();
            String[] receiverIdArr= receiverIdStr.split(",");
            for(int i=0 ;i<receiverIdArr.length;i++){
                receiverIdList.add(Integer.parseInt(receiverIdArr[i]));
            }

            transportRecordDao.updateTransportRecordDataPrice(price,sendTimeKsrq,sendTimeJsrq,varietiesIdList,supplierIdList,receiverIdList);
            return ResponseData.build(200,"保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.build(500,"保存失败");
        }
    }


    public ResponseData updateIsSettleAccounts(Integer isSettleAccounts, String sendTimeKsrq, String sendTimeJsrq, String varietiesIdStr, String supplierIdStr, String receiverIdStr) {
        try{

            List<Integer> varietiesIdList = new ArrayList<>();
            String[] varietiesIdArr= varietiesIdStr.split(",");
            for(int i=0 ;i<varietiesIdArr.length;i++){
                varietiesIdList.add(Integer.parseInt(varietiesIdArr[i]));
            }

            List<Integer> supplierIdList = new ArrayList<>();
            String[] supplierIdArr= supplierIdStr.split(",");
            for(int i=0 ;i<supplierIdArr.length;i++){
                supplierIdList.add(Integer.parseInt(supplierIdArr[i]));
            }

            List<Integer> receiverIdList = new ArrayList<>();
            String[] receiverIdArr= receiverIdStr.split(",");
            for(int i=0 ;i<receiverIdArr.length;i++){
                receiverIdList.add(Integer.parseInt(receiverIdArr[i]));
            }

            transportRecordDao.updateIsSettleAccounts(isSettleAccounts,sendTimeKsrq,sendTimeJsrq,varietiesIdList,supplierIdList,receiverIdList);
            return ResponseData.build(200,"保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.build(500,"保存失败");
        }
    }
}
